<template>
    <div class="dl">
        <span>密码验证</span>
        <span>手机验证</span>

        <el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" :rules="rules" label-width="auto"
            class="demo-ruleForm" :size="formSize" status-icon>
            <el-form-item label="手机号" prop="username">
                <el-input v-model="ruleForm.username" />
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input v-model="ruleForm.password" />
            </el-form-item>
            <el-button type="primary" @click="submitForm(ruleFormRef)">
                Create
            </el-button>
        </el-form>
    </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { loginApi } from '@/api/api'
import { ElMessage } from 'element-plus'
import {useRouter} from 'vue-router'
let router=new useRouter()
const ruleFormRef = ref()
const ruleForm = reactive({
    username: '',
    password: ''
})
const rules = reactive({
    username: [
        { required: true, message: '请输入手机号', trigger: 'blur' },
        { min: 3, max: 11, message: 'Length should be 9 to 11', trigger: 'blur' },
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 3, max: 8, message: 'Length should be 3 to 5', trigger: 'blur' },
    ],
})
const submitForm = async (formEl) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            loginApi(ruleForm.username, ruleForm.password).then(res => {
                console.log('登录数据', res)
                localStorage.setItem('accessToken', res.data.data.accessToken)
                localStorage.setItem('refreshToken', res.data.data.refreshToken)
                localStorage.setItem('username', res.data.data.username)
                localStorage.setItem('avator', res.data.data.avator)
                if (res.data.code == 10000) {
                    ElMessage({
                        message: '登录成功.',
                        type: 'success',
                    })
                    router.push('/home')
                }
            })
        } else {
            console.log('error submit!', fields)
        }
    })
}

</script>

<style>
.dl {
    width: 300px;
    height: 300px;
    border: 1px solid red;
    margin-left: 100px;
}

.dl span {
    display: inline-block;
    width: 30%;
    margin-left: 10px;

}
</style>